1 //
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 //
16 
17 #ifndef UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
18 #define UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
19 
20 #include <stdint.h>
21 
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 #include <base/macros.h>
27 #include <base/time/time.h>
28 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
29 
30 #include "update_engine/common/constants.h"
31 #include "update_engine/common/platform_constants.h"
32 #include "update_engine/image_properties.h"
33 
34 // This gathers local system information and prepares info used by the
35 // Omaha request action.
36 
37 namespace chromeos_update_engine {
38 
39 class SystemState;
40 
41 // 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 //
45 // TODO(jaysri): chromium-os:39752 tracks the need to rename this class to
46 // reflect its lifetime more appropriately.
47 class OmahaRequestParams {
48  public:
OmahaRequestParams(SystemState * system_state)49   explicit OmahaRequestParams(SystemState* system_state)
50       : system_state_(system_state),
51         os_platform_(constants::kOmahaPlatformName),
52         os_version_(kOsVersion),
53         delta_okay_(true),
54         interactive_(false),
55         rollback_allowed_(false),
56         rollback_data_save_requested_(false),
57         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_install_(false) {}
62 
63   virtual ~OmahaRequestParams();
64 
65   enum ActiveCountingType {
66     kDayBased = 0,
67     kDateBased,
68   };
69 
70   struct AppParams {
71     ActiveCountingType active_counting_type;
72     // |name| is only used for DLCs to store the DLC ID.
73     std::string name;
74     int64_t ping_active;
75     int64_t ping_date_last_active;
76     int64_t ping_date_last_rollcall;
77     bool send_ping;
78     // |updated| is only used for DLCs to decide sending DBus message to
79     // dlcservice on an install/update completion.
80     bool updated = true;
81   };
82 
83   // Setters and getters for the various properties.
os_platform()84   inline std::string os_platform() const { return os_platform_; }
os_version()85   inline std::string os_version() const { return os_version_; }
os_sp()86   inline std::string os_sp() const { return os_sp_; }
os_board()87   inline std::string os_board() const { return image_props_.board; }
os_build_fingerprint()88   inline std::string os_build_fingerprint() const {
89     return image_props_.build_fingerprint;
90   }
os_build_type()91   inline std::string os_build_type() const { return image_props_.build_type; }
board_app_id()92   inline std::string board_app_id() const { return image_props_.product_id; }
canary_app_id()93   inline std::string canary_app_id() const {
94     return image_props_.canary_product_id;
95   }
system_app_id()96   inline std::string system_app_id() const { return image_props_.system_id; }
set_system_app_id(const std::string & system_app_id)97   inline void set_system_app_id(const std::string& system_app_id) {
98     image_props_.system_id = system_app_id;
99   }
set_app_id(const std::string & app_id)100   inline void set_app_id(const std::string& app_id) {
101     image_props_.product_id = app_id;
102     image_props_.canary_product_id = app_id;
103   }
app_lang()104   inline std::string app_lang() const { return app_lang_; }
hwid()105   inline std::string hwid() const { return hwid_; }
fw_version()106   inline std::string fw_version() const { return fw_version_; }
ec_version()107   inline std::string ec_version() const { return ec_version_; }
device_requisition()108   inline std::string device_requisition() const { return device_requisition_; }
109 
set_app_version(const std::string & version)110   inline void set_app_version(const std::string& version) {
111     image_props_.version = version;
112   }
app_version()113   inline std::string app_version() const { return image_props_.version; }
system_version()114   inline std::string system_version() const {
115     return image_props_.system_version;
116   }
product_components()117   inline std::string product_components() const {
118     return image_props_.product_components;
119   }
set_product_components(const std::string & product_components)120   inline void set_product_components(const std::string& product_components) {
121     image_props_.product_components = product_components;
122   }
123 
current_channel()124   inline std::string current_channel() const {
125     return image_props_.current_channel;
126   }
target_channel()127   inline std::string target_channel() const {
128     return mutable_image_props_.target_channel;
129   }
download_channel()130   inline std::string download_channel() const { return download_channel_; }
131 
132   // Can client accept a delta ?
set_delta_okay(bool ok)133   inline void set_delta_okay(bool ok) { delta_okay_ = ok; }
delta_okay()134   inline bool delta_okay() const { return delta_okay_; }
135 
136   // True if this is a user-initiated update check.
set_interactive(bool interactive)137   inline void set_interactive(bool interactive) { interactive_ = interactive; }
interactive()138   inline bool interactive() const { return interactive_; }
139 
set_update_url(const std::string & url)140   inline void set_update_url(const std::string& url) { update_url_ = url; }
update_url()141   inline std::string update_url() const { return update_url_; }
142 
set_target_version_prefix(const std::string & prefix)143   inline void set_target_version_prefix(const std::string& prefix) {
144     target_version_prefix_ = prefix;
145   }
146 
target_version_prefix()147   inline std::string target_version_prefix() const {
148     return target_version_prefix_;
149   }
150 
set_rollback_allowed(bool rollback_allowed)151   inline void set_rollback_allowed(bool rollback_allowed) {
152     rollback_allowed_ = rollback_allowed;
153   }
154 
rollback_allowed()155   inline bool rollback_allowed() const { return rollback_allowed_; }
156 
set_rollback_data_save_requested(bool rollback_data_save_requested)157   inline void set_rollback_data_save_requested(
158       bool rollback_data_save_requested) {
159     rollback_data_save_requested_ = rollback_data_save_requested;
160   }
161 
rollback_data_save_requested()162   inline bool rollback_data_save_requested() const {
163     return rollback_data_save_requested_;
164   }
165 
set_rollback_allowed_milestones(int rollback_allowed_milestones)166   inline void set_rollback_allowed_milestones(int rollback_allowed_milestones) {
167     rollback_allowed_milestones_ = rollback_allowed_milestones;
168   }
169 
rollback_allowed_milestones()170   inline int rollback_allowed_milestones() const {
171     return rollback_allowed_milestones_;
172   }
173 
set_wall_clock_based_wait_enabled(bool enabled)174   inline void set_wall_clock_based_wait_enabled(bool enabled) {
175     wall_clock_based_wait_enabled_ = enabled;
176   }
wall_clock_based_wait_enabled()177   inline bool wall_clock_based_wait_enabled() const {
178     return wall_clock_based_wait_enabled_;
179   }
180 
set_waiting_period(base::TimeDelta period)181   inline void set_waiting_period(base::TimeDelta period) {
182     waiting_period_ = period;
183   }
waiting_period()184   base::TimeDelta waiting_period() const { return waiting_period_; }
185 
set_update_check_count_wait_enabled(bool enabled)186   inline void set_update_check_count_wait_enabled(bool enabled) {
187     update_check_count_wait_enabled_ = enabled;
188   }
189 
update_check_count_wait_enabled()190   inline bool update_check_count_wait_enabled() const {
191     return update_check_count_wait_enabled_;
192   }
193 
set_min_update_checks_needed(int64_t min)194   inline void set_min_update_checks_needed(int64_t min) {
195     min_update_checks_needed_ = min;
196   }
min_update_checks_needed()197   inline int64_t min_update_checks_needed() const {
198     return min_update_checks_needed_;
199   }
200 
set_max_update_checks_allowed(int64_t max)201   inline void set_max_update_checks_allowed(int64_t max) {
202     max_update_checks_allowed_ = max;
203   }
max_update_checks_allowed()204   inline int64_t max_update_checks_allowed() const {
205     return max_update_checks_allowed_;
206   }
set_dlc_apps_params(const std::map<std::string,AppParams> & dlc_apps_params)207   inline void set_dlc_apps_params(
208       const std::map<std::string, AppParams>& dlc_apps_params) {
209     dlc_apps_params_ = dlc_apps_params;
210   }
dlc_apps_params()211   inline const std::map<std::string, AppParams>& dlc_apps_params() const {
212     return dlc_apps_params_;
213   }
set_is_install(bool is_install)214   inline void set_is_install(bool is_install) { is_install_ = is_install; }
is_install()215   inline bool is_install() const { return is_install_; }
216 
set_autoupdate_token(const std::string & token)217   inline void set_autoupdate_token(const std::string& token) {
218     autoupdate_token_ = token;
219   }
autoupdate_token()220   inline const std::string& autoupdate_token() const {
221     return autoupdate_token_;
222   }
223 
224   // Returns the App ID corresponding to the current value of the
225   // download channel.
226   virtual std::string GetAppId() const;
227 
228   // Returns the DLC app ID.
229   virtual std::string GetDlcAppId(const std::string& dlc_id) const;
230 
231   // Returns true if the App ID is a DLC App ID that is currently part of the
232   // request parameters.
233   virtual bool IsDlcAppId(const std::string& app_id) const;
234 
235   // If the App ID is a DLC App ID will set to no update.
236   void SetDlcNoUpdate(const std::string& app_id);
237 
238   // Suggested defaults
239   static const char kOsVersion[];
240   static const int64_t kDefaultMinUpdateChecks = 0;
241   static const int64_t kDefaultMaxUpdateChecks = 8;
242 
243   // Initializes all the data in the object. Non-empty
244   // |in_app_version| or |in_update_url| prevents automatic detection
245   // of the parameter. Returns true on success, false otherwise.
246   bool Init(const std::string& in_app_version,
247             const std::string& in_update_url,
248             bool in_interactive);
249 
250   // Permanently changes the release channel to |channel|. Performs a
251   // powerwash, if required and allowed.
252   // Returns true on success, false otherwise. Note: This call will fail if
253   // there's a channel change pending already. This is to serialize all the
254   // channel changes done by the user in order to avoid having to solve
255   // numerous edge cases around ensuring the powerwash happens as intended in
256   // all such cases.
257   virtual bool SetTargetChannel(const std::string& channel,
258                                 bool is_powerwash_allowed,
259                                 std::string* error_message);
260 
261   // Updates the download channel for this particular attempt from the current
262   // value of target channel.  This method takes a "snapshot" of the current
263   // value of target channel and uses it for all subsequent Omaha requests for
264   // this attempt (i.e. initial request as well as download progress/error
265   // event requests). The snapshot will be updated only when either this method
266   // or Init is called again.
267   virtual void UpdateDownloadChannel();
268 
269   // Returns whether we should powerwash for this update. Note that this is
270   // just an indication, the final decision to powerwash or not is made in the
271   // response handler.
272   bool ShouldPowerwash() const;
273 
274   // Check if the provided update URL is official, meaning either the default
275   // autoupdate server or the autoupdate autotest server.
276   virtual bool IsUpdateUrlOfficial() const;
277 
278   // For unit-tests.
279   void set_root(const std::string& root);
set_current_channel(const std::string & channel)280   void set_current_channel(const std::string& channel) {
281     image_props_.current_channel = channel;
282   }
set_target_channel(const std::string & channel)283   void set_target_channel(const std::string& channel) {
284     mutable_image_props_.target_channel = channel;
285   }
set_os_sp(const std::string & os_sp)286   void set_os_sp(const std::string& os_sp) { os_sp_ = os_sp; }
set_os_board(const std::string & os_board)287   void set_os_board(const std::string& os_board) {
288     image_props_.board = os_board;
289   }
set_app_lang(const std::string & app_lang)290   void set_app_lang(const std::string& app_lang) { app_lang_ = app_lang; }
set_hwid(const std::string & hwid)291   void set_hwid(const std::string& hwid) { hwid_ = hwid; }
set_fw_version(const std::string & fw_version)292   void set_fw_version(const std::string& fw_version) {
293     fw_version_ = fw_version;
294   }
set_ec_version(const std::string & ec_version)295   void set_ec_version(const std::string& ec_version) {
296     ec_version_ = ec_version;
297   }
set_is_powerwash_allowed(bool powerwash_allowed)298   void set_is_powerwash_allowed(bool powerwash_allowed) {
299     mutable_image_props_.is_powerwash_allowed = powerwash_allowed;
300   }
set_device_requisition(const std::string & requisition)301   void set_device_requisition(const std::string& requisition) {
302     device_requisition_ = requisition;
303   }
304 
305  private:
306   FRIEND_TEST(OmahaRequestParamsTest, ChannelIndexTest);
307   FRIEND_TEST(OmahaRequestParamsTest, CollectECFWVersionsTest);
308   FRIEND_TEST(OmahaRequestParamsTest, IsValidChannelTest);
309   FRIEND_TEST(OmahaRequestParamsTest, SetIsPowerwashAllowedTest);
310   FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelInvalidTest);
311   FRIEND_TEST(OmahaRequestParamsTest, SetTargetChannelTest);
312   FRIEND_TEST(OmahaRequestParamsTest, ShouldPowerwashTest);
313   FRIEND_TEST(OmahaRequestParamsTest, ToMoreStableChannelFlagTest);
314 
315   // Returns true if |channel| is a valid channel, otherwise write error to
316   // |error_message| if passed and return false.
317   bool IsValidChannel(const std::string& channel,
318                       std::string* error_message) const;
IsValidChannel(const std::string & channel)319   bool IsValidChannel(const std::string& channel) const {
320     return IsValidChannel(channel, nullptr);
321   }
322 
323   // Returns the index of the given channel.
324   int GetChannelIndex(const std::string& channel) const;
325 
326   // True if we're trying to update to a more stable channel.
327   // i.e. index(target_channel) > index(current_channel).
328   bool ToMoreStableChannel() const;
329 
330   // Returns True if we should store the fw/ec versions based on our hwid_.
331   // Compares hwid to a set of prefixes in the allowlist.
332   bool CollectECFWVersions() const;
333 
334   // Gets the machine type (e.g. "i686").
335   std::string GetMachineType() const;
336 
337   // Global system context.
338   SystemState* system_state_;
339 
340   // The system image properties.
341   ImageProperties image_props_;
342   MutableImageProperties mutable_image_props_;
343 
344   // Basic properties of the OS and Application that go into the Omaha request.
345   std::string os_platform_;
346   std::string os_version_;
347   std::string os_sp_;
348   std::string app_lang_;
349 
350   // There are three channel values we deal with:
351   // * The channel we got the image we are running from or "current channel"
352   //   stored in |image_props_.current_channel|.
353   //
354   // * The release channel we are tracking, where we should get updates from,
355   //   stored in |mutable_image_props_.target_channel|. This channel is
356   //   normally the same as the current_channel, except when the user changes
357   //   the channel. In that case it'll have the release channel the user
358   //   switched to, regardless of whether we downloaded an update from that
359   //   channel or not, or if we are in the middle of a download from a
360   //   previously selected channel  (as opposed to download channel
361   //   which gets updated only at the start of next download).
362   //
363   // * The channel from which we're downloading the payload. This should
364   //   normally be the same as target channel. But if the user made another
365   //   channel change after we started the download, then they'd be different,
366   //   in which case, we'd detect elsewhere that the target channel has been
367   //   changed and cancel the current download attempt.
368   std::string download_channel_;
369 
370   std::string hwid_;        // Hardware Qualification ID of the client
371   std::string fw_version_;  // Chrome OS Firmware Version.
372   std::string ec_version_;  // Chrome OS EC Version.
373   // TODO(b:133324571) tracks removal of this field once it is no longer
374   // needed in AU requests. Remove by October 1st 2019.
375   std::string device_requisition_;  // Chrome OS Requisition type.
376   bool delta_okay_;         // If this client can accept a delta
377   bool interactive_;        // Whether this is a user-initiated update check
378 
379   // The URL to send the Omaha request to.
380   std::string update_url_;
381 
382   // Prefix of the target OS version that the enterprise wants this device
383   // to be pinned to. It's empty otherwise.
384   std::string target_version_prefix_;
385 
386   // Whether the client is accepting rollback images too.
387   bool rollback_allowed_;
388 
389   // Whether rollbacks should preserve some system state during powerwash.
390   bool rollback_data_save_requested_;
391 
392   // How many milestones the client can rollback to.
393   int rollback_allowed_milestones_;
394 
395   // True if scattering or staging are enabled, in which case waiting_period_
396   // specifies the amount of absolute time that we've to wait for before sending
397   // a request to Omaha.
398   bool wall_clock_based_wait_enabled_;
399   base::TimeDelta waiting_period_;
400 
401   // True if scattering or staging are enabled to denote the number of update
402   // checks we've to skip before we can send a request to Omaha. The min and max
403   // values establish the bounds for a random number to be chosen within that
404   // range to enable such a wait.
405   bool update_check_count_wait_enabled_;
406   int64_t min_update_checks_needed_;
407   int64_t max_update_checks_allowed_;
408 
409   // When reading files, prepend root_ to the paths. Useful for testing.
410   std::string root_;
411 
412   // A list of DLC modules to install. A mapping from DLC App ID to |AppParams|.
413   std::map<std::string, AppParams> dlc_apps_params_;
414 
415   // This variable defines whether the payload is being installed in the current
416   // partition. At the moment, this is used for installing DLC modules on the
417   // current active partition instead of the inactive partition.
418   bool is_install_;
419 
420   // Token used when making an update request for a specific build.
421   // For example: Token for a Quick Fix Build:
422   // https://cloud.google.com/docs/chrome-enterprise/policies/?policy=DeviceQuickFixBuildToken
423   std::string autoupdate_token_;
424 
425   DISALLOW_COPY_AND_ASSIGN(OmahaRequestParams);
426 };
427 
428 }  // namespace chromeos_update_engine
429 
430 #endif  // UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H_
431