blob: 502f7b0602e50c64a7e1ef2667295950c029185d [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// 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>
13
14#include "base/string_util.h"
15#include "update_engine/simple_key_value_store.h"
16#include "update_engine/utils.h"
17
18using std::map;
19using std::string;
20
21namespace {
22const string OmahaIdPath() {
23 return string(chromeos_update_engine::utils::kStatefulPartition) +
24 "/etc/omaha_id";
25}
26} // namespace {}
27
28namespace chromeos_update_engine {
29
30bool OmahaRequestDeviceParams::Init() {
31 TEST_AND_RETURN_FALSE(GetMachineId(&machine_id));
32 user_id = machine_id;
33 os_platform = OmahaRequestParams::kOsPlatform;
34 os_version = OmahaRequestParams::kOsVersion;
35 app_version = GetLsbValue("CHROMEOS_RELEASE_VERSION", "");
36 os_sp = app_version + "_" + GetMachineType();
37 os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "");
38 app_id = OmahaRequestParams::kAppId;
39 app_lang = "en-US";
40 app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", "");
41 update_url = GetLsbValue("CHROMEOS_AUSERVER",
42 OmahaRequestParams::kUpdateUrl);
43 return true;
44}
45
46namespace {
47const size_t kGuidDataByteLength = 128 / 8;
48const string::size_type kGuidStringLength = 38;
49// Formats 16 bytes (128 bits) of data as a GUID:
50// "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit
51string GuidFromData(const unsigned char data[kGuidDataByteLength]) {
52 return StringPrintf(
53 "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
54 data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
55 data[8], data[9], data[10], data[11], data[12], data[13], data[14],
56 data[15]);
57}
58}
59
60// Returns true on success.
61bool OmahaRequestDeviceParams::GetMachineId(std::string* out_id) const {
62 // Checks if we have an existing Machine ID.
63 const string omaha_id_path = root_ + OmahaIdPath();
64
65 if (utils::ReadFileToString(omaha_id_path, out_id) &&
66 out_id->size() == kGuidStringLength) {
67 return true;
68 }
69
70 // Creates a new ID.
71 int rand_fd = open("/dev/urandom", O_RDONLY, 0);
72 TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0);
73 ScopedFdCloser rand_fd_closer(&rand_fd);
74 unsigned char buf[kGuidDataByteLength];
75 size_t bytes_read = 0;
76 while (bytes_read < sizeof(buf)) {
77 ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read);
78 TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
79 bytes_read += rc;
80 }
81 string guid = GuidFromData(buf);
82 TEST_AND_RETURN_FALSE(
83 utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size()));
84 *out_id = guid;
85 return true;
86}
87
88string OmahaRequestDeviceParams::GetLsbValue(
89 const string& key, const string& default_value) const {
90 string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release",
91 "/etc/lsb-release"};
92 for (unsigned int i = 0; i < arraysize(files); ++i) {
93 // TODO(adlr): make sure files checked are owned as root (and all
94 // their parents are recursively, too).
95 string file_data;
96 if (!utils::ReadFileToString(root_ + files[i], &file_data))
97 continue;
98
99 map<string, string> data = simple_key_value_store::ParseString(file_data);
100 if (utils::MapContainsKey(data, key))
101 return data[key];
102 }
103 // not found
104 return default_value;
105}
106
107string OmahaRequestDeviceParams::GetMachineType() const {
108 struct utsname buf;
109 string ret;
110 if (uname(&buf) == 0)
111 ret = buf.machine;
112 return ret;
113}
114
115} // namespace chromeos_update_engine