blob: 25772d03ae6493eb4c72bc7571c1deef9fe90bd4 [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 The Chromium 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_prep_action.h"
6#include <sys/utsname.h>
Andrew de los Reyes970bb282009-12-09 16:34:04 -08007#include <errno.h>
Andrew de los Reyes785bc352010-05-26 12:34:53 -07008#include <map>
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <string>
Andrew de los Reyes970bb282009-12-09 16:34:04 -080010#include "base/string_util.h"
Andrew de los Reyes785bc352010-05-26 12:34:53 -070011#include "update_engine/simple_key_value_store.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000012#include "update_engine/utils.h"
13
Andrew de los Reyes785bc352010-05-26 12:34:53 -070014using std::map;
adlr@google.com3defe6a2009-12-04 20:57:17 +000015using std::string;
16
17// This gathers local system information and prepares info used by the
18// update check action.
19
Andrew de los Reyes970bb282009-12-09 16:34:04 -080020namespace {
21const string OmahaIdPath() {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080022 return string(chromeos_update_engine::utils::kStatefulPartition) +
23 "/etc/omaha_id";
Andrew de los Reyes970bb282009-12-09 16:34:04 -080024}
25} // namespace {}
26
adlr@google.com3defe6a2009-12-04 20:57:17 +000027namespace chromeos_update_engine {
28
29void OmahaRequestPrepAction::PerformAction() {
30 // TODO(adlr): honor force_full_update_
Andrew de los Reyes970bb282009-12-09 16:34:04 -080031 ScopedActionCompleter completer(processor_, this);
32 string machine_id;
33 TEST_AND_RETURN(GetMachineId(&machine_id));
Andrew de los Reyesf9714432010-05-04 10:21:23 -070034 const string version(GetLsbValue("CHROMEOS_RELEASE_VERSION", ""));
adlr@google.com3defe6a2009-12-04 20:57:17 +000035 const string sp(version + "_" + GetMachineType());
Andrew de los Reyesf9714432010-05-04 10:21:23 -070036 const string track(GetLsbValue("CHROMEOS_RELEASE_TRACK", ""));
37 const string update_url(GetLsbValue("CHROMEOS_AUSERVER",
38 UpdateCheckParams::kUpdateUrl));
adlr@google.com3defe6a2009-12-04 20:57:17 +000039
40 UpdateCheckParams out(machine_id, // machine_id
41 machine_id, // user_id (use machine_id)
42 UpdateCheckParams::kOsPlatform,
43 UpdateCheckParams::kOsVersion,
44 sp, // e.g. 0.2.3.3_i686
45 UpdateCheckParams::kAppId,
46 version, // app version (from lsb-release)
Andrew de los Reyesf9714432010-05-04 10:21:23 -070047 "en-US", // lang
48 track, // track
Andrew de los Reyesf98bff82010-05-06 13:33:25 -070049 update_url);
adlr@google.com3defe6a2009-12-04 20:57:17 +000050
51 CHECK(HasOutputPipe());
52 SetOutputObject(out);
Andrew de los Reyes970bb282009-12-09 16:34:04 -080053 completer.set_success(true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000054}
55
Andrew de los Reyes970bb282009-12-09 16:34:04 -080056namespace {
57const size_t kGuidDataByteLength = 128 / 8;
58const string::size_type kGuidStringLength = 38;
59// Formats 16 bytes (128 bits) of data as a GUID:
60// "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit
61string GuidFromData(const unsigned char data[kGuidDataByteLength]) {
62 return StringPrintf(
63 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
64 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
65 data[8], data[9], data[10], data[11], data[12], data[13], data[14],
66 data[15]);
67}
68}
69
70// Returns true on success.
71bool OmahaRequestPrepAction::GetMachineId(std::string* out_id) const {
72 // See if we have an existing Machine ID
73 const string omaha_id_path = root_ + OmahaIdPath();
74
75 if (utils::ReadFileToString(omaha_id_path, out_id) &&
76 out_id->size() == kGuidStringLength) {
77 return true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000078 }
Andrew de los Reyes970bb282009-12-09 16:34:04 -080079
80 // Create a new ID
81 int rand_fd = open("/dev/urandom", O_RDONLY, 0);
82 TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0);
83 ScopedFdCloser rand_fd_closer(&rand_fd);
84 unsigned char buf[kGuidDataByteLength];
85 size_t bytes_read = 0;
86 while (bytes_read < sizeof(buf)) {
87 ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read);
88 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
89 bytes_read += rc;
90 }
91 string guid = GuidFromData(buf);
92 TEST_AND_RETURN_FALSE(
93 utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size()));
94 *out_id = guid;
95 return true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000096}
97
Andrew de los Reyesf9714432010-05-04 10:21:23 -070098string OmahaRequestPrepAction::GetLsbValue(
99 const string& key, const string& default_value) const {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800100 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release",
adlr@google.com3defe6a2009-12-04 20:57:17 +0000101 "/etc/lsb-release"};
102 for (unsigned int i = 0; i < arraysize(files); i++) {
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700103 // TODO(adlr): make sure files checked are owned as root (and all
104 // their parents are recursively, too).
adlr@google.com3defe6a2009-12-04 20:57:17 +0000105 string file_data;
106 if (!utils::ReadFileToString(root_ + files[i], &file_data))
107 continue;
Andrew de los Reyes785bc352010-05-26 12:34:53 -0700108
109 map<string, string> data = simple_key_value_store::ParseString(file_data);
110 if (utils::MapContainsKey(data, key))
111 return data[key];
adlr@google.com3defe6a2009-12-04 20:57:17 +0000112 }
113 // not found
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700114 return default_value;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000115}
116
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700117string OmahaRequestPrepAction::GetMachineType() const {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000118 struct utsname buf;
119 string ret;
120 if (uname(&buf) == 0)
121 ret = buf.machine;
122 return ret;
123}
124
125} // namespace chromeos_update_engine