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