omaha_request_params.cc revision f2065b4d77be793fe0a5b4280fe23433cbecaa1b
1// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/omaha_request_params.h"
6
7#include <errno.h>
8#include <fcntl.h>
9#include <sys/utsname.h>
10
11#include <map>
12#include <string>
13#include <vector>
14
15#include <base/file_util.h>
16
17#include "update_engine/simple_key_value_store.h"
18#include "update_engine/utils.h"
19
20#define CALL_MEMBER_FN(object, member) ((object).*(member))
21
22using std::map;
23using std::string;
24using std::vector;
25
26namespace chromeos_update_engine {
27
28const char OmahaRequestParams::kUpdateTrackKey[] = "CHROMEOS_RELEASE_TRACK";
29
30const char* const OmahaRequestParams::kAppId(
31    "{87efface-864d-49a5-9bb3-4b050a7c227a}");
32const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
33const char* const OmahaRequestParams::kOsVersion("Indy");
34const char* const OmahaRequestParams::kUpdateUrl(
35    "https://tools.google.com/service/update2");
36
37OmahaRequestDeviceParams::OmahaRequestDeviceParams() :
38    force_lock_down_(false),
39    forced_lock_down_(false) {}
40
41bool OmahaRequestDeviceParams::Init(const std::string& in_app_version,
42                                    const std::string& in_update_url) {
43  bool stateful_override = !ShouldLockDown();
44  os_platform = OmahaRequestParams::kOsPlatform;
45  os_version = OmahaRequestParams::kOsVersion;
46  app_version = in_app_version.empty() ?
47      GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL, stateful_override) :
48      in_app_version;
49  os_sp = app_version + "_" + GetMachineType();
50  os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "", NULL, stateful_override);
51  app_id = GetLsbValue("CHROMEOS_RELEASE_APPID",
52                       OmahaRequestParams::kAppId,
53                       NULL,
54                       stateful_override);
55  app_lang = "en-US";
56  app_track = GetLsbValue(
57      kUpdateTrackKey,
58      "",
59      &chromeos_update_engine::OmahaRequestDeviceParams::IsValidTrack,
60      true);  // stateful_override
61  hardware_class = utils::GetHardwareClass();
62  struct stat stbuf;
63
64  // Deltas are only okay if the /.nodelta file does not exist.  If we don't
65  // know (i.e. stat() returns some unexpected error), then err on the side of
66  // caution and say deltas are not okay.
67  delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
68               (errno == ENOENT);
69
70  // For now, disable delta updates if the rootfs track is different than the
71  // track that we're sending to the update server because such updates are
72  // destined to fail -- the source rootfs hash will be different than the
73  // expected hash due to the different track in /etc/lsb-release.
74  //
75  // Longer term we should consider an alternative: (a) clients can send
76  // (current_version, current_channel, new_channel) information, or (b) the
77  // build process can make sure releases on separate tracks are identical (i.e,
78  // by not stamping the release with the channel), or (c) the release process
79  // can ensure that different channels get different version numbers.
80  const string rootfs_track = GetLsbValue(
81      kUpdateTrackKey,
82      "",
83      NULL,  // No need to validate the read-only rootfs track.
84      false);  // stateful_override
85  delta_okay = delta_okay && rootfs_track == app_track;
86
87  update_url = in_update_url.empty() ?
88      GetLsbValue("CHROMEOS_AUSERVER",
89                  OmahaRequestParams::kUpdateUrl,
90                  NULL,
91                  stateful_override) :
92      in_update_url;
93  return true;
94}
95
96bool OmahaRequestDeviceParams::SetTrack(const std::string& track) {
97  TEST_AND_RETURN_FALSE(IsValidTrack(track));
98  FilePath kFile(root_ + utils::kStatefulPartition + "/etc/lsb-release");
99  string file_data;
100  map<string, string> data;
101  if (file_util::ReadFileToString(kFile, &file_data)) {
102    data = simple_key_value_store::ParseString(file_data);
103  }
104  data[kUpdateTrackKey] = track;
105  file_data = simple_key_value_store::AssembleString(data);
106  TEST_AND_RETURN_FALSE(file_util::CreateDirectory(kFile.DirName()));
107  TEST_AND_RETURN_FALSE(
108      file_util::WriteFile(kFile, file_data.data(), file_data.size()) ==
109      static_cast<int>(file_data.size()));
110  app_track = track;
111  return true;
112}
113
114bool OmahaRequestDeviceParams::SetDeviceTrack(const std::string& track) {
115  OmahaRequestDeviceParams params;
116  TEST_AND_RETURN_FALSE(params.Init("", ""));
117  return params.SetTrack(track);
118}
119
120string OmahaRequestDeviceParams::GetDeviceTrack() {
121  OmahaRequestDeviceParams params;
122  // Note that params.app_track is an empty string if the value in
123  // lsb-release file is invalid. See Init() for details.
124  return params.Init("", "") ? params.app_track : "";
125}
126
127string OmahaRequestDeviceParams::GetLsbValue(const string& key,
128                                             const string& default_value,
129                                             ValueValidator validator,
130                                             bool stateful_override) const {
131  vector<string> files;
132  if (stateful_override) {
133    files.push_back(string(utils::kStatefulPartition) + "/etc/lsb-release");
134  }
135  files.push_back("/etc/lsb-release");
136  for (vector<string>::const_iterator it = files.begin();
137       it != files.end(); ++it) {
138    // TODO(adlr): make sure files checked are owned as root (and all their
139    // parents are recursively, too).
140    string file_data;
141    if (!utils::ReadFileToString(root_ + *it, &file_data))
142      continue;
143
144    map<string, string> data = simple_key_value_store::ParseString(file_data);
145    if (utils::MapContainsKey(data, key)) {
146      const string& value = data[key];
147      if (validator && !CALL_MEMBER_FN(*this, validator)(value)) {
148        continue;
149      }
150      return value;
151    }
152  }
153  // not found
154  return default_value;
155}
156
157string OmahaRequestDeviceParams::GetMachineType() const {
158  struct utsname buf;
159  string ret;
160  if (uname(&buf) == 0)
161    ret = buf.machine;
162  return ret;
163}
164
165bool OmahaRequestDeviceParams::ShouldLockDown() const {
166  if (force_lock_down_) {
167    return forced_lock_down_;
168  }
169  return utils::IsOfficialBuild() && utils::IsNormalBootMode();
170}
171
172bool OmahaRequestDeviceParams::IsValidTrack(const std::string& track) const {
173  static const char* kValidTracks[] = {
174    "canary-channel",
175    "stable-channel",
176    "beta-channel",
177    "dev-channel",
178  };
179  if (!ShouldLockDown()) {
180    return true;
181  }
182  for (size_t t = 0; t < arraysize(kValidTracks); ++t) {
183    if (track == kValidTracks[t]) {
184      return true;
185    }
186  }
187  return false;
188}
189
190void OmahaRequestDeviceParams::SetLockDown(bool lock) {
191  force_lock_down_ = true;
192  forced_lock_down_ = lock;
193}
194
195}  // namespace chromeos_update_engine
196