blob: 0f5af5bc60d3020023c34754cea5c0e801cf9c3f [file] [log] [blame]
Darin Petkovf2065b42011-05-17 16:36:27 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Darin Petkova4a8a8c2010-07-15 22:21:12 -07002// 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>
Darin Petkova3df55b2010-11-15 13:33:55 -080013#include <vector>
Darin Petkova4a8a8c2010-07-15 22:21:12 -070014
Darin Petkov49d91322010-10-25 16:34:58 -070015#include <base/file_util.h>
Darin Petkov49d91322010-10-25 16:34:58 -070016
Darin Petkova4a8a8c2010-07-15 22:21:12 -070017#include "update_engine/simple_key_value_store.h"
18#include "update_engine/utils.h"
19
Darin Petkov49d91322010-10-25 16:34:58 -070020#define CALL_MEMBER_FN(object, member) ((object).*(member))
21
Darin Petkova4a8a8c2010-07-15 22:21:12 -070022using std::map;
23using std::string;
Darin Petkova3df55b2010-11-15 13:33:55 -080024using std::vector;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070025
Darin Petkova4a8a8c2010-07-15 22:21:12 -070026namespace chromeos_update_engine {
27
Darin Petkov49d91322010-10-25 16:34:58 -070028const char OmahaRequestParams::kUpdateTrackKey[] = "CHROMEOS_RELEASE_TRACK";
29
Darin Petkov5a7f5652010-07-22 21:40:09 -070030const 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
Darin Petkov49d91322010-10-25 16:34:58 -070037OmahaRequestDeviceParams::OmahaRequestDeviceParams() :
Darin Petkov10d02dd2011-01-10 14:57:39 -080038 force_lock_down_(false),
39 forced_lock_down_(false) {}
Darin Petkov49d91322010-10-25 16:34:58 -070040
Darin Petkov5a7f5652010-07-22 21:40:09 -070041bool OmahaRequestDeviceParams::Init(const std::string& in_app_version,
42 const std::string& in_update_url) {
Darin Petkov10d02dd2011-01-10 14:57:39 -080043 bool stateful_override = !ShouldLockDown();
Darin Petkova4a8a8c2010-07-15 22:21:12 -070044 os_platform = OmahaRequestParams::kOsPlatform;
45 os_version = OmahaRequestParams::kOsVersion;
Darin Petkov5a7f5652010-07-22 21:40:09 -070046 app_version = in_app_version.empty() ?
Darin Petkov10d02dd2011-01-10 14:57:39 -080047 GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL, stateful_override) :
48 in_app_version;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070049 os_sp = app_version + "_" + GetMachineType();
Darin Petkov10d02dd2011-01-10 14:57:39 -080050 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "", NULL, stateful_override);
Darin Petkovd315dc72010-11-15 09:52:24 -080051 app_id = GetLsbValue("CHROMEOS_RELEASE_APPID",
52 OmahaRequestParams::kAppId,
Darin Petkova3df55b2010-11-15 13:33:55 -080053 NULL,
Darin Petkov10d02dd2011-01-10 14:57:39 -080054 stateful_override);
Darin Petkova4a8a8c2010-07-15 22:21:12 -070055 app_lang = "en-US";
Darin Petkov49d91322010-10-25 16:34:58 -070056 app_track = GetLsbValue(
57 kUpdateTrackKey,
58 "",
Darin Petkova3df55b2010-11-15 13:33:55 -080059 &chromeos_update_engine::OmahaRequestDeviceParams::IsValidTrack,
Darin Petkov10d02dd2011-01-10 14:57:39 -080060 true); // stateful_override
Darin Petkovf2065b42011-05-17 16:36:27 -070061 hardware_class = utils::GetHardwareClass();
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070062 struct stat stbuf;
Darin Petkov5a7f5652010-07-22 21:40:09 -070063
Darin Petkova3df55b2010-11-15 13:33:55 -080064 // 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.
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -070067 delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) &&
68 (errno == ENOENT);
69
Darin Petkova3df55b2010-11-15 13:33:55 -080070 // 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 "",
Darin Petkov10d02dd2011-01-10 14:57:39 -080083 NULL, // No need to validate the read-only rootfs track.
84 false); // stateful_override
Darin Petkova3df55b2010-11-15 13:33:55 -080085 delta_okay = delta_okay && rootfs_track == app_track;
86
Darin Petkov5a7f5652010-07-22 21:40:09 -070087 update_url = in_update_url.empty() ?
Darin Petkova3df55b2010-11-15 13:33:55 -080088 GetLsbValue("CHROMEOS_AUSERVER",
89 OmahaRequestParams::kUpdateUrl,
90 NULL,
Darin Petkov10d02dd2011-01-10 14:57:39 -080091 stateful_override) :
Darin Petkov5a7f5652010-07-22 21:40:09 -070092 in_update_url;
Darin Petkova4a8a8c2010-07-15 22:21:12 -070093 return true;
94}
95
Darin Petkov49d91322010-10-25 16:34:58 -070096bool 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
Satoru Takabayashi583667b2010-10-27 13:09:57 +0900120string 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
Darin Petkov49d91322010-10-25 16:34:58 -0700127string OmahaRequestDeviceParams::GetLsbValue(const string& key,
128 const string& default_value,
Darin Petkova3df55b2010-11-15 13:33:55 -0800129 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).
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700140 string file_data;
Darin Petkova3df55b2010-11-15 13:33:55 -0800141 if (!utils::ReadFileToString(root_ + *it, &file_data))
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700142 continue;
143
144 map<string, string> data = simple_key_value_store::ParseString(file_data);
Darin Petkov49d91322010-10-25 16:34:58 -0700145 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 }
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700152 }
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
Darin Petkov10d02dd2011-01-10 14:57:39 -0800165bool OmahaRequestDeviceParams::ShouldLockDown() const {
166 if (force_lock_down_) {
167 return forced_lock_down_;
168 }
169 return utils::IsOfficialBuild() && utils::IsNormalBootMode();
Darin Petkov49d91322010-10-25 16:34:58 -0700170}
171
172bool OmahaRequestDeviceParams::IsValidTrack(const std::string& track) const {
Darin Petkov3f375c72010-11-05 09:42:26 -0700173 static const char* kValidTracks[] = {
174 "canary-channel",
Andrew de los Reyesdc928762011-04-12 13:15:25 -0700175 "stable-channel",
Darin Petkov3f375c72010-11-05 09:42:26 -0700176 "beta-channel",
177 "dev-channel",
178 };
Darin Petkov10d02dd2011-01-10 14:57:39 -0800179 if (!ShouldLockDown()) {
Darin Petkov3f375c72010-11-05 09:42:26 -0700180 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;
Darin Petkov49d91322010-10-25 16:34:58 -0700188}
189
Darin Petkov10d02dd2011-01-10 14:57:39 -0800190void OmahaRequestDeviceParams::SetLockDown(bool lock) {
191 force_lock_down_ = true;
192 forced_lock_down_ = lock;
Darin Petkov49d91322010-10-25 16:34:58 -0700193}
194
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700195} // namespace chromeos_update_engine