| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "update_engine/omaha_request_params.h" | 5 #include "update_engine/omaha_request_params.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/utsname.h> | 9 #include <sys/utsname.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "update_engine/simple_key_value_store.h" | 15 #include "update_engine/simple_key_value_store.h" |
| 16 #include "update_engine/utils.h" | 16 #include "update_engine/utils.h" |
| 17 | 17 |
| 18 using std::map; | 18 using std::map; |
| 19 using std::string; | 19 using std::string; |
| 20 | 20 |
| 21 namespace { | |
| 22 const string OmahaIdPath() { | |
| 23 return string(chromeos_update_engine::utils::kStatefulPartition) + | |
| 24 "/etc/omaha_id"; | |
| 25 } | |
| 26 } // namespace {} | |
| 27 | |
| 28 namespace chromeos_update_engine { | 21 namespace chromeos_update_engine { |
| 29 | 22 |
| 30 const char* const OmahaRequestParams::kAppId( | 23 const char* const OmahaRequestParams::kAppId( |
| 31 "{87efface-864d-49a5-9bb3-4b050a7c227a}"); | 24 "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
| 32 const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); | 25 const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
| 33 const char* const OmahaRequestParams::kOsVersion("Indy"); | 26 const char* const OmahaRequestParams::kOsVersion("Indy"); |
| 34 const char* const OmahaRequestParams::kUpdateUrl( | 27 const char* const OmahaRequestParams::kUpdateUrl( |
| 35 "https://tools.google.com/service/update2"); | 28 "https://tools.google.com/service/update2"); |
| 36 | 29 |
| 37 bool OmahaRequestDeviceParams::Init(const std::string& in_app_version, | 30 bool OmahaRequestDeviceParams::Init(const std::string& in_app_version, |
| 38 const std::string& in_update_url) { | 31 const std::string& in_update_url) { |
| 39 TEST_AND_RETURN_FALSE(GetMachineId(&machine_id)); | |
| 40 user_id = machine_id; | |
| 41 os_platform = OmahaRequestParams::kOsPlatform; | 32 os_platform = OmahaRequestParams::kOsPlatform; |
| 42 os_version = OmahaRequestParams::kOsVersion; | 33 os_version = OmahaRequestParams::kOsVersion; |
| 43 app_version = in_app_version.empty() ? | 34 app_version = in_app_version.empty() ? |
| 44 GetLsbValue("CHROMEOS_RELEASE_VERSION", "") : in_app_version; | 35 GetLsbValue("CHROMEOS_RELEASE_VERSION", "") : in_app_version; |
| 45 os_sp = app_version + "_" + GetMachineType(); | 36 os_sp = app_version + "_" + GetMachineType(); |
| 46 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", ""); | 37 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", ""); |
| 47 app_id = OmahaRequestParams::kAppId; | 38 app_id = OmahaRequestParams::kAppId; |
| 48 app_lang = "en-US"; | 39 app_lang = "en-US"; |
| 49 app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", ""); | 40 app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", ""); |
| 50 struct stat stbuf; | 41 struct stat stbuf; |
| 51 | 42 |
| 52 // Deltas are only okay if the /.nodelta file does not exist. | 43 // Deltas are only okay if the /.nodelta file does not exist. |
| 53 // If we don't know (i.e. stat() returns some unexpected error), | 44 // If we don't know (i.e. stat() returns some unexpected error), |
| 54 // then err on the side of caution and say deltas are not okay | 45 // then err on the side of caution and say deltas are not okay |
| 55 delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && | 46 delta_okay = (stat((root_ + "/.nodelta").c_str(), &stbuf) < 0) && |
| 56 (errno == ENOENT); | 47 (errno == ENOENT); |
| 57 | 48 |
| 58 update_url = in_update_url.empty() ? | 49 update_url = in_update_url.empty() ? |
| 59 GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl) : | 50 GetLsbValue("CHROMEOS_AUSERVER", OmahaRequestParams::kUpdateUrl) : |
| 60 in_update_url; | 51 in_update_url; |
| 61 return true; | 52 return true; |
| 62 } | 53 } |
| 63 | 54 |
| 64 namespace { | |
| 65 const size_t kGuidDataByteLength = 128 / 8; | |
| 66 const string::size_type kGuidStringLength = 38; | |
| 67 // Formats 16 bytes (128 bits) of data as a GUID: | |
| 68 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit | |
| 69 string GuidFromData(const unsigned char data[kGuidDataByteLength]) { | |
| 70 return StringPrintf( | |
| 71 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}", | |
| 72 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], | |
| 73 data[8], data[9], data[10], data[11], data[12], data[13], data[14], | |
| 74 data[15]); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 // Returns true on success. | |
| 79 bool OmahaRequestDeviceParams::GetMachineId(std::string* out_id) const { | |
| 80 // Checks if we have an existing Machine ID. | |
| 81 const string omaha_id_path = root_ + OmahaIdPath(); | |
| 82 | |
| 83 if (utils::ReadFileToString(omaha_id_path, out_id) && | |
| 84 out_id->size() == kGuidStringLength) { | |
| 85 return true; | |
| 86 } | |
| 87 | |
| 88 // Creates a new ID. | |
| 89 int rand_fd = open("/dev/urandom", O_RDONLY, 0); | |
| 90 TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0); | |
| 91 ScopedFdCloser rand_fd_closer(&rand_fd); | |
| 92 unsigned char buf[kGuidDataByteLength]; | |
| 93 size_t bytes_read = 0; | |
| 94 while (bytes_read < sizeof(buf)) { | |
| 95 ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read); | |
| 96 TEST_AND_RETURN_FALSE_ERRNO(rc > 0); | |
| 97 bytes_read += rc; | |
| 98 } | |
| 99 string guid = GuidFromData(buf); | |
| 100 TEST_AND_RETURN_FALSE( | |
| 101 utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size())); | |
| 102 *out_id = guid; | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 string OmahaRequestDeviceParams::GetLsbValue( | 55 string OmahaRequestDeviceParams::GetLsbValue( |
| 107 const string& key, const string& default_value) const { | 56 const string& key, const string& default_value) const { |
| 108 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", | 57 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release", |
| 109 "/etc/lsb-release"}; | 58 "/etc/lsb-release"}; |
| 110 for (unsigned int i = 0; i < arraysize(files); ++i) { | 59 for (unsigned int i = 0; i < arraysize(files); ++i) { |
| 111 // TODO(adlr): make sure files checked are owned as root (and all | 60 // TODO(adlr): make sure files checked are owned as root (and all |
| 112 // their parents are recursively, too). | 61 // their parents are recursively, too). |
| 113 string file_data; | 62 string file_data; |
| 114 if (!utils::ReadFileToString(root_ + files[i], &file_data)) | 63 if (!utils::ReadFileToString(root_ + files[i], &file_data)) |
| 115 continue; | 64 continue; |
| 116 | 65 |
| 117 map<string, string> data = simple_key_value_store::ParseString(file_data); | 66 map<string, string> data = simple_key_value_store::ParseString(file_data); |
| 118 if (utils::MapContainsKey(data, key)) | 67 if (utils::MapContainsKey(data, key)) |
| 119 return data[key]; | 68 return data[key]; |
| 120 } | 69 } |
| 121 // not found | 70 // not found |
| 122 return default_value; | 71 return default_value; |
| 123 } | 72 } |
| 124 | 73 |
| 125 string OmahaRequestDeviceParams::GetMachineType() const { | 74 string OmahaRequestDeviceParams::GetMachineType() const { |
| 126 struct utsname buf; | 75 struct utsname buf; |
| 127 string ret; | 76 string ret; |
| 128 if (uname(&buf) == 0) | 77 if (uname(&buf) == 0) |
| 129 ret = buf.machine; | 78 ret = buf.machine; |
| 130 return ret; | 79 return ret; |
| 131 } | 80 } |
| 132 | 81 |
| 133 } // namespace chromeos_update_engine | 82 } // namespace chromeos_update_engine |
| OLD | NEW |