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