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> |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 16 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 17 | #include "update_engine/simple_key_value_store.h" |
| 18 | #include "update_engine/utils.h" |
| 19 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 20 | #define CALL_MEMBER_FN(object, member) ((object).*(member)) |
| 21 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 22 | using std::map; |
| 23 | using std::string; |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 24 | using std::vector; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 25 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 26 | namespace chromeos_update_engine { |
| 27 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 28 | const char OmahaRequestParams::kUpdateTrackKey[] = "CHROMEOS_RELEASE_TRACK"; |
| 29 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 30 | const char* const OmahaRequestParams::kAppId( |
| 31 | "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
| 32 | const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
| 33 | const char* const OmahaRequestParams::kOsVersion("Indy"); |
| 34 | const char* const OmahaRequestParams::kUpdateUrl( |
| 35 | "https://tools.google.com/service/update2"); |
| 36 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 37 | OmahaRequestDeviceParams::OmahaRequestDeviceParams() : |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 38 | force_lock_down_(false), |
| 39 | forced_lock_down_(false) {} |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 40 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 41 | bool OmahaRequestDeviceParams::Init(const std::string& in_app_version, |
| 42 | const std::string& in_update_url) { |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 43 | bool stateful_override = !ShouldLockDown(); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 44 | os_platform = OmahaRequestParams::kOsPlatform; |
| 45 | os_version = OmahaRequestParams::kOsVersion; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 46 | app_version = in_app_version.empty() ? |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 47 | GetLsbValue("CHROMEOS_RELEASE_VERSION", "", NULL, stateful_override) : |
| 48 | in_app_version; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 49 | os_sp = app_version + "_" + GetMachineType(); |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 50 | os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "", NULL, stateful_override); |
Darin Petkov | d315dc7 | 2010-11-15 09:52:24 -0800 | [diff] [blame] | 51 | app_id = GetLsbValue("CHROMEOS_RELEASE_APPID", |
| 52 | OmahaRequestParams::kAppId, |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 53 | NULL, |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 54 | stateful_override); |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 55 | app_lang = "en-US"; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 56 | app_track = GetLsbValue( |
| 57 | kUpdateTrackKey, |
| 58 | "", |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 59 | &chromeos_update_engine::OmahaRequestDeviceParams::IsValidTrack, |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 60 | true); // stateful_override |
Darin Petkov | f2065b4 | 2011-05-17 16:36:27 -0700 | [diff] [blame^] | 61 | hardware_class = utils::GetHardwareClass(); |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 62 | struct stat stbuf; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 63 | |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 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. |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 67 | delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
| 68 | (errno == ENOENT); |
| 69 | |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 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 | "", |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 83 | NULL, // No need to validate the read-only rootfs track. |
| 84 | false); // stateful_override |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 85 | delta_okay = delta_okay && rootfs_track == app_track; |
| 86 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 87 | update_url = in_update_url.empty() ? |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 88 | GetLsbValue("CHROMEOS_AUSERVER", |
| 89 | OmahaRequestParams::kUpdateUrl, |
| 90 | NULL, |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 91 | stateful_override) : |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame] | 92 | in_update_url; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 96 | bool 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 | |
| 114 | bool OmahaRequestDeviceParams::SetDeviceTrack(const std::string& track) { |
| 115 | OmahaRequestDeviceParams params; |
| 116 | TEST_AND_RETURN_FALSE(params.Init("", "")); |
| 117 | return params.SetTrack(track); |
| 118 | } |
| 119 | |
Satoru Takabayashi | 583667b | 2010-10-27 13:09:57 +0900 | [diff] [blame] | 120 | string 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 Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 127 | string OmahaRequestDeviceParams::GetLsbValue(const string& key, |
| 128 | const string& default_value, |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 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). |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 140 | string file_data; |
Darin Petkov | a3df55b | 2010-11-15 13:33:55 -0800 | [diff] [blame] | 141 | if (!utils::ReadFileToString(root_ + *it, &file_data)) |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 142 | continue; |
| 143 | |
| 144 | map<string, string> data = simple_key_value_store::ParseString(file_data); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 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 | } |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 152 | } |
| 153 | // not found |
| 154 | return default_value; |
| 155 | } |
| 156 | |
| 157 | string 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 Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 165 | bool OmahaRequestDeviceParams::ShouldLockDown() const { |
| 166 | if (force_lock_down_) { |
| 167 | return forced_lock_down_; |
| 168 | } |
| 169 | return utils::IsOfficialBuild() && utils::IsNormalBootMode(); |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | bool OmahaRequestDeviceParams::IsValidTrack(const std::string& track) const { |
Darin Petkov | 3f375c7 | 2010-11-05 09:42:26 -0700 | [diff] [blame] | 173 | static const char* kValidTracks[] = { |
| 174 | "canary-channel", |
Andrew de los Reyes | dc92876 | 2011-04-12 13:15:25 -0700 | [diff] [blame] | 175 | "stable-channel", |
Darin Petkov | 3f375c7 | 2010-11-05 09:42:26 -0700 | [diff] [blame] | 176 | "beta-channel", |
| 177 | "dev-channel", |
| 178 | }; |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 179 | if (!ShouldLockDown()) { |
Darin Petkov | 3f375c7 | 2010-11-05 09:42:26 -0700 | [diff] [blame] | 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; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Darin Petkov | 10d02dd | 2011-01-10 14:57:39 -0800 | [diff] [blame] | 190 | void OmahaRequestDeviceParams::SetLockDown(bool lock) { |
| 191 | force_lock_down_ = true; |
| 192 | forced_lock_down_ = lock; |
Darin Petkov | 49d9132 | 2010-10-25 16:34:58 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 195 | } // namespace chromeos_update_engine |