Darin Petkov | f2065b4 | 2011-05-17 16:36:27 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 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> |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 13 | #include <vector> |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 14 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 15 | #include <base/file_util.h> |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 16 | #include <policy/device_policy.h> |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 17 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame^] | 18 | #include "update_engine/constants.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 19 | #include "update_engine/simple_key_value_store.h" |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 20 | #include "update_engine/system_state.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 21 | #include "update_engine/utils.h" |
| 22 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 23 | #define CALL_MEMBER_FN(object, member) ((object).*(member)) |
| 24 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 25 | using std::map; |
| 26 | using std::string; |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 27 | using std::vector; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 28 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 29 | namespace chromeos_update_engine { |
| 30 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 31 | const char* const OmahaRequestParams::kAppId( |
| 32 | "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
| 33 | const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
| 34 | const char* const OmahaRequestParams::kOsVersion("Indy"); |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 35 | const char* const kProductionOmahaUrl( |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 36 | "https://tools.google.com/service/update2"); |
| 37 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 38 | const char* const OmahaRequestParams::kUpdateChannelKey( |
| 39 | "CHROMEOS_RELEASE_TRACK"); |
| 40 | const char* const OmahaRequestParams::kIsPowerwashAllowedKey( |
| 41 | "CHROMEOS_IS_POWERWASH_ALLOWED"); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 42 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 43 | const char* kChannelsByStability[] = { |
| 44 | // This list has to be sorted from least stable to most stable channel. |
| 45 | "canary-channel", |
| 46 | "dev-channel", |
| 47 | "beta-channel", |
| 48 | "stable-channel", |
| 49 | }; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 50 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 51 | bool OmahaRequestParams::Init(const std::string& in_app_version, |
| 52 | const std::string& in_update_url, |
| 53 | bool in_interactive) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 54 | LOG(INFO) << "Initializing parameters for this update attempt"; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 55 | InitFromLsbValue(); |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 56 | bool stateful_override = !ShouldLockDown(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 57 | os_platform_ = OmahaRequestParams::kOsPlatform; |
| 58 | os_version_ = OmahaRequestParams::kOsVersion; |
| 59 | app_version_ = in_app_version.empty() ? |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 60 | GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL, stateful_override) : |
| 61 | in_app_version; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 62 | os_sp_ = app_version_ + "_" + GetMachineType(); |
| 63 | os_board_ = GetLsbValue("CHROMEOS_RELEASE_BOARD", |
| 64 | "", |
| 65 | NULL, |
| 66 | stateful_override); |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 67 | string release_app_id = GetLsbValue("CHROMEOS_RELEASE_APPID", |
| 68 | OmahaRequestParams::kAppId, |
| 69 | NULL, |
| 70 | stateful_override); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 71 | board_app_id_ = GetLsbValue("CHROMEOS_BOARD_APPID", |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 72 | release_app_id, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 73 | NULL, |
| 74 | stateful_override); |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 75 | canary_app_id_ = GetLsbValue("CHROMEOS_CANARY_APPID", |
| 76 | release_app_id, |
| 77 | NULL, |
| 78 | stateful_override); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 79 | app_lang_ = "en-US"; |
| 80 | hwid_ = utils::GetHardwareClass(); |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 81 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 82 | if (current_channel_ == target_channel_) { |
| 83 | // deltas are only okay if the /.nodelta file does not exist. if we don't |
| 84 | // know (i.e. stat() returns some unexpected error), then err on the side of |
| 85 | // caution and say deltas are not okay. |
| 86 | struct stat stbuf; |
| 87 | delta_okay_ = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
| 88 | (errno == ENOENT); |
| 89 | |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 90 | } else { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 91 | LOG(INFO) << "Disabling deltas as a channel change is pending"; |
| 92 | // For now, disable delta updates if the current channel is different from |
| 93 | // the channel that we're sending to the update server because such updates |
| 94 | // are destined to fail -- the current rootfs hash will be different than |
| 95 | // the expected hash due to the different channel in /etc/lsb-release. |
| 96 | delta_okay_ = false; |
Patrick Dubroy | 7fbbe8a | 2011-08-01 17:28:22 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 99 | if (in_update_url.empty()) |
| 100 | update_url_ = GetLsbValue("CHROMEOS_AUSERVER", kProductionOmahaUrl, NULL, |
| 101 | stateful_override); |
| 102 | else |
| 103 | update_url_ = in_update_url; |
Gilad Arnold | bbdd490 | 2013-01-10 16:06:30 -0800 | [diff] [blame] | 104 | |
| 105 | // Set the interactive flag accordingly. |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 106 | interactive_ = in_interactive; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 110 | bool OmahaRequestParams::SetTargetChannel(const std::string& new_target_channel, |
| 111 | bool is_powerwash_allowed) { |
| 112 | LOG(INFO) << "SetTargetChannel called with " << new_target_channel |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 113 | << ", Is Powerwash Allowed = " |
| 114 | << utils::ToString(is_powerwash_allowed) |
| 115 | << ". Current channel = " << current_channel_ |
| 116 | << ", existing target channel = " << target_channel_ |
| 117 | << ", download channel = " << download_channel_; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 118 | |
| 119 | if (current_channel_ == "canary-channel") { |
| 120 | // TODO(jaysri): chromium-os:39751: We don't have the UI warnings yet. So, |
| 121 | // enable the powerwash-on-changing-to-more-stable-channel behavior for now |
| 122 | // only on canary-channel devices. |
| 123 | is_powerwash_allowed = true; |
| 124 | LOG(INFO) << "Is Powerwash Allowed set to true as we are in canary-channel"; |
| 125 | } else if (!utils::IsOfficialBuild() && |
| 126 | current_channel_ == "testimage-channel") { |
| 127 | // Also, allow test builds to have the powerwash behavior so we can always |
| 128 | // test channel changing behavior on them, without having to first get them |
| 129 | // on an official channel. |
| 130 | is_powerwash_allowed = true; |
| 131 | LOG(INFO) << "Is Powerwash Allowed set to true as we are running an " |
| 132 | "unofficial build"; |
| 133 | } |
| 134 | |
| 135 | TEST_AND_RETURN_FALSE(IsValidChannel(new_target_channel)); |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame^] | 136 | FilePath kFile(root_ + kStatefulPartition + "/etc/lsb-release"); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 137 | string file_data; |
| 138 | map<string, string> data; |
| 139 | if (file_util::ReadFileToString(kFile, &file_data)) { |
| 140 | data = simple_key_value_store::ParseString(file_data); |
| 141 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 142 | data[kUpdateChannelKey] = new_target_channel; |
| 143 | data[kIsPowerwashAllowedKey] = is_powerwash_allowed ? "true" : "false"; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 144 | file_data = simple_key_value_store::AssembleString(data); |
| 145 | TEST_AND_RETURN_FALSE(file_util::CreateDirectory(kFile.DirName())); |
| 146 | TEST_AND_RETURN_FALSE( |
| 147 | file_util::WriteFile(kFile, file_data.data(), file_data.size()) == |
| 148 | static_cast<int>(file_data.size())); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 149 | target_channel_ = new_target_channel; |
| 150 | is_powerwash_allowed_ = is_powerwash_allowed; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 154 | void OmahaRequestParams::SetTargetChannelFromLsbValue() { |
| 155 | string target_channel_new_value = GetLsbValue( |
| 156 | kUpdateChannelKey, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 157 | current_channel_, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 158 | &chromeos_update_engine::OmahaRequestParams::IsValidChannel, |
| 159 | true); // stateful_override |
| 160 | |
| 161 | if (target_channel_ != target_channel_new_value) { |
| 162 | target_channel_ = target_channel_new_value; |
| 163 | LOG(INFO) << "Target Channel set to " << target_channel_ |
| 164 | << " from LSB file"; |
| 165 | } |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 168 | void OmahaRequestParams::SetCurrentChannelFromLsbValue() { |
| 169 | string current_channel_new_value = GetLsbValue( |
| 170 | kUpdateChannelKey, |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 171 | current_channel_, |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 172 | NULL, // No need to validate the read-only rootfs channel. |
| 173 | false); // stateful_override is false so we get the current channel. |
| 174 | |
| 175 | if (current_channel_ != current_channel_new_value) { |
| 176 | current_channel_ = current_channel_new_value; |
| 177 | LOG(INFO) << "Current Channel set to " << current_channel_ |
| 178 | << " from LSB file in rootfs"; |
| 179 | } |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 180 | } |
| 181 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 182 | void OmahaRequestParams::SetIsPowerwashAllowedFromLsbValue() { |
| 183 | string is_powerwash_allowed_str = GetLsbValue( |
| 184 | kIsPowerwashAllowedKey, |
| 185 | "false", |
| 186 | NULL, // no need to validate |
| 187 | true); // always get it from stateful, as that's the only place it'll be |
| 188 | bool is_powerwash_allowed_new_value = (is_powerwash_allowed_str == "true"); |
| 189 | if (is_powerwash_allowed_ != is_powerwash_allowed_new_value) { |
| 190 | is_powerwash_allowed_ = is_powerwash_allowed_new_value; |
| 191 | LOG(INFO) << "Powerwash Allowed set to " |
| 192 | << utils::ToString(is_powerwash_allowed_) |
| 193 | << " from LSB file in stateful"; |
| 194 | } |
| 195 | } |
| 196 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 197 | void OmahaRequestParams::UpdateDownloadChannel() { |
| 198 | if (download_channel_ != target_channel_) { |
| 199 | download_channel_ = target_channel_; |
| 200 | LOG(INFO) << "Download channel for this attempt = " << download_channel_; |
| 201 | } |
| 202 | } |
| 203 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 204 | void OmahaRequestParams::InitFromLsbValue() { |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 205 | SetCurrentChannelFromLsbValue(); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 206 | SetTargetChannelFromLsbValue(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 207 | SetIsPowerwashAllowedFromLsbValue(); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 208 | UpdateDownloadChannel(); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | string OmahaRequestParams::GetLsbValue(const string& key, |
| 212 | const string& default_value, |
| 213 | ValueValidator validator, |
| 214 | bool stateful_override) const { |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 215 | vector<string> files; |
| 216 | if (stateful_override) { |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame^] | 217 | files.push_back(string(kStatefulPartition) + "/etc/lsb-release"); |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 218 | } |
| 219 | files.push_back("/etc/lsb-release"); |
| 220 | for (vector<string>::const_iterator it = files.begin(); |
| 221 | it != files.end(); ++it) { |
| 222 | // TODO(adlr): make sure files checked are owned as root (and all their |
| 223 | // parents are recursively, too). |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 224 | string file_data; |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 225 | if (!utils::ReadFile(root_ + *it, &file_data)) |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 226 | continue; |
| 227 | |
| 228 | map<string, string> data = simple_key_value_store::ParseString(file_data); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 229 | if (utils::MapContainsKey(data, key)) { |
| 230 | const string& value = data[key]; |
| 231 | if (validator && !CALL_MEMBER_FN(*this, validator)(value)) { |
| 232 | continue; |
| 233 | } |
| 234 | return value; |
| 235 | } |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 236 | } |
| 237 | // not found |
| 238 | return default_value; |
| 239 | } |
| 240 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 241 | string OmahaRequestParams::GetMachineType() const { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 242 | struct utsname buf; |
| 243 | string ret; |
| 244 | if (uname(&buf) == 0) |
| 245 | ret = buf.machine; |
| 246 | return ret; |
| 247 | } |
| 248 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 249 | bool OmahaRequestParams::ShouldLockDown() const { |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 250 | if (force_lock_down_) { |
| 251 | return forced_lock_down_; |
| 252 | } |
| 253 | return utils::IsOfficialBuild() && utils::IsNormalBootMode(); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 256 | bool OmahaRequestParams::IsValidChannel(const std::string& channel) const { |
| 257 | return GetChannelIndex(channel) >= 0; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 260 | void OmahaRequestParams::set_root(const std::string& root) { |
| 261 | root_ = root; |
| 262 | InitFromLsbValue(); |
| 263 | } |
| 264 | |
| 265 | void OmahaRequestParams::SetLockDown(bool lock) { |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 266 | force_lock_down_ = true; |
| 267 | forced_lock_down_ = lock; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 270 | int OmahaRequestParams::GetChannelIndex(const std::string& channel) const { |
| 271 | for (size_t t = 0; t < arraysize(kChannelsByStability); ++t) |
| 272 | if (channel == kChannelsByStability[t]) |
| 273 | return t; |
| 274 | |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | bool OmahaRequestParams::to_more_stable_channel() const { |
| 279 | int current_channel_index = GetChannelIndex(current_channel_); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 280 | int download_channel_index = GetChannelIndex(download_channel_); |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 281 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 282 | return download_channel_index > current_channel_index; |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jay Srinivasan | db0acdf | 2013-04-02 14:47:45 -0700 | [diff] [blame] | 285 | string OmahaRequestParams::GetAppId() const { |
| 286 | return download_channel_ == "canary-channel" ? canary_app_id_ : board_app_id_; |
| 287 | } |
| 288 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 289 | } // namespace chromeos_update_engine |