File: daemon_controller_delegate_linux.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (281 lines) | stat: -rw-r--r-- 8,769 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/setup/daemon_controller_delegate_linux.h"

#include <unistd.h>

#include <optional>
#include <utility>

#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "build/build_config.h"
#include "remoting/base/file_path_util_linux.h"
#include "remoting/host/host_config.h"
#include "remoting/host/usage_stats_consent.h"

namespace remoting {

namespace {

#ifndef NDEBUG
const char kDaemonDevScript[] = "remoting/chrome-remote-desktop";
#endif  // NDEBUG

const char kDaemonScript[] =
    "/opt/google/chrome-remote-desktop/chrome-remote-desktop";

// The name of the command-line switch used to specify the host configuration
// file to use.
const char kHostConfigSwitchName[] = "host-config";

bool start_host_after_setup = true;

base::FilePath GetConfigPath() {
  base::CommandLine* current_process = base::CommandLine::ForCurrentProcess();
  if (current_process->HasSwitch(kHostConfigSwitchName)) {
    return current_process->GetSwitchValuePath(kHostConfigSwitchName);
  }
  std::string filename = GetHostHash() + ".json";
  return GetConfigDirectoryPath().Append(filename);
}

bool GetScriptPath(base::FilePath* result) {
#ifndef NDEBUG
  base::FilePath out_dir;
  base::PathService::Get(base::DIR_EXE, &out_dir);
  base::FilePath dev_exe = out_dir.AppendASCII(kDaemonDevScript);
  if (access(dev_exe.value().c_str(), X_OK) == 0) {
    *result = dev_exe;
    return true;
  }
#endif  // NDEBUG

  base::FilePath candidate_exe(kDaemonScript);
  if (access(candidate_exe.value().c_str(), X_OK) == 0) {
    *result = candidate_exe;
    return true;
  }
  return false;
}

bool RunHostScript(const std::vector<std::string>& args) {
  // As long as we're relying on running an external binary from the
  // PATH, don't do it as root.
  if (getuid() == 0) {
    LOG(ERROR) << "Refusing to run script as root.";
    return false;
  }
  base::FilePath script_path;
  if (!GetScriptPath(&script_path)) {
    LOG(ERROR) << "GetScriptPath() failed.";
    return false;
  }
  base::CommandLine command_line(script_path);
  for (unsigned int i = 0; i < args.size(); ++i) {
    command_line.AppendArg(args[i]);
  }

  std::string output;
  bool result = base::GetAppOutputAndError(command_line, &output);
  if (result) {
    LOG(INFO) << output;
  } else {
    LOG(ERROR) << output;
  }

  return result;
}

}  // namespace

DaemonControllerDelegateLinux::DaemonControllerDelegateLinux() = default;

DaemonControllerDelegateLinux::~DaemonControllerDelegateLinux() = default;

DaemonController::State DaemonControllerDelegateLinux::GetState() {
  base::FilePath script_path;
  if (!GetScriptPath(&script_path)) {
    LOG(ERROR) << "GetScriptPath() failed.";
    return DaemonController::STATE_UNKNOWN;
  }
  base::CommandLine command_line(script_path);
  command_line.AppendArg("--get-status");
  command_line.AppendArg("--config=" + GetConfigPath().value());

  std::string status;
  int exit_code = 0;
  if (!base::GetAppOutputWithExitCode(command_line, &status, &exit_code) ||
      exit_code != 0) {
    LOG(ERROR) << "Failed to run \"" << command_line.GetCommandLineString()
               << "\". Exit code: " << exit_code;
    return DaemonController::STATE_UNKNOWN;
  }

  base::TrimWhitespaceASCII(status, base::TRIM_ALL, &status);

  if (status == "STARTED") {
    return DaemonController::STATE_STARTED;
  } else if (status == "STOPPED") {
    return DaemonController::STATE_STOPPED;
  } else if (status == "NOT_IMPLEMENTED") {
    // Chrome Remote Desktop is not currently supported on the underlying Linux
    // Distro.
    return DaemonController::STATE_NOT_IMPLEMENTED;
  } else {
    LOG(ERROR) << "Unknown status string returned from  \""
               << command_line.GetCommandLineString() << "\": " << status;
    return DaemonController::STATE_UNKNOWN;
  }
}

std::optional<base::Value::Dict> DaemonControllerDelegateLinux::GetConfig() {
  std::optional<base::Value::Dict> host_config(
      HostConfigFromJsonFile(GetConfigPath()));
  if (!host_config.has_value()) {
    return std::nullopt;
  }

  base::Value::Dict result;
  std::string* value = host_config->FindString(kHostIdConfigPath);
  if (value) {
    result.Set(kHostIdConfigPath, *value);
  }

  value = host_config->FindString(kServiceAccountConfigPath);
  if (value) {
    // Set both keys for compatibility purposes.
    result.Set(kServiceAccountConfigPath, *value);
    result.Set(kDeprecatedXmppLoginConfigPath, *value);
  }

  return result;
}

void DaemonControllerDelegateLinux::CheckPermission(
    bool it2me,
    DaemonController::BoolCallback callback) {
  std::move(callback).Run(true);
}

void DaemonControllerDelegateLinux::SetConfigAndStart(
    base::Value::Dict config,
    bool consent,
    DaemonController::CompletionCallback done) {
  // Ensure the configuration directory exists.
  base::FilePath config_dir = GetConfigPath().DirName();
  if (!base::DirectoryExists(config_dir)) {
    base::File::Error error;
    if (!base::CreateDirectoryAndGetError(config_dir, &error)) {
      LOG(ERROR) << "Failed to create config directory " << config_dir.value()
                 << ", error: " << base::File::ErrorToString(error);
      std::move(done).Run(DaemonController::RESULT_FAILED);
      return;
    }
  }

  // Write config.
  if (!HostConfigToJsonFile(std::move(config), GetConfigPath())) {
    LOG(ERROR) << "Failed to update config file.";
    std::move(done).Run(DaemonController::RESULT_FAILED);
    return;
  }

  if (start_host_after_setup) {
    // Finally start the host.
    std::vector<std::string> args = {"--enable-and-start"};

    // TODO(rkjnsn): At this point, the host is configured and just requires an
    // administrator to enable and start it. If that fails here, e.g., due to no
    // policy kit agent running, it might be nice to tell the user what they
    // need to do so they can perform the last step manually (or have an
    // administrator do it, if the user isn't one).
    if (!RunHostScript(args)) {
      LOG(ERROR) << "Failed to start host.";
      std::move(done).Run(DaemonController::RESULT_FAILED);
      return;
    }
  }

  std::move(done).Run(DaemonController::RESULT_OK);
}

void DaemonControllerDelegateLinux::UpdateConfig(
    base::Value::Dict config,
    DaemonController::CompletionCallback done) {
  std::optional<base::Value::Dict> new_config(
      HostConfigFromJsonFile(GetConfigPath()));
  if (!new_config.has_value()) {
    LOG(ERROR) << "Failed to read existing config file.";
    std::move(done).Run(DaemonController::RESULT_FAILED);
    return;
  }

  new_config->Merge(std::move(config));

  if (!HostConfigToJsonFile(std::move(*new_config), GetConfigPath())) {
    LOG(ERROR) << "Failed to update config file.";
    std::move(done).Run(DaemonController::RESULT_FAILED);
    return;
  }

  std::vector<std::string> args = {"--reload",
                                   "--config=" + GetConfigPath().value()};
  DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
  if (RunHostScript(args)) {
    result = DaemonController::RESULT_OK;
  }

  std::move(done).Run(result);
}

void DaemonControllerDelegateLinux::Stop(
    DaemonController::CompletionCallback done) {
  std::vector<std::string> args = {"--stop",
                                   "--config=" + GetConfigPath().value()};
  DaemonController::AsyncResult result = DaemonController::RESULT_FAILED;
  if (RunHostScript(args)) {
    result = DaemonController::RESULT_OK;
  }

  std::move(done).Run(result);
}

DaemonController::UsageStatsConsent
DaemonControllerDelegateLinux::GetUsageStatsConsent() {
  // Crash dump collection is not implemented on Linux yet.
  // http://crbug.com/130678.
  DaemonController::UsageStatsConsent consent;
  consent.supported = false;
  consent.allowed = false;
  consent.set_by_policy = false;
  return consent;
}

void DaemonControllerDelegateLinux::set_start_host_after_setup(
    bool start_host) {
  start_host_after_setup = start_host;
}

scoped_refptr<DaemonController> DaemonController::Create() {
  return new DaemonController(
      base::WrapUnique(new DaemonControllerDelegateLinux()));
}

}  // namespace remoting