1 // Copyright 2013 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.
5 #include "remoting/host/setup/daemon_controller_delegate_linux.h"
9 #include "base/base_paths.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/environment.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/json/json_writer.h"
18 #include "base/logging.h"
20 #include "base/path_service.h"
21 #include "base/process/launch.h"
22 #include "base/process/process.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_split.h"
25 #include "base/strings/string_util.h"
26 #include "base/thread_task_runner_handle.h"
27 #include "base/values.h"
28 #include "build/build_config.h"
29 #include "net/base/net_util.h"
30 #include "remoting/host/host_config.h"
31 #include "remoting/host/usage_stats_consent.h"
37 const char kDaemonScript
[] =
38 "/opt/google/chrome-remote-desktop/chrome-remote-desktop";
40 // Timeout for running daemon script. The script itself sets a timeout when
41 // waiting for the host to come online, so the setting here should be at least
43 const int64 kDaemonTimeoutMs
= 60000;
45 // Timeout for commands that require password prompt - 5 minutes.
46 const int64 kSudoTimeoutSeconds
= 5 * 60;
48 std::string
GetMd5(const std::string
& value
) {
51 base::MD5Update(&ctx
, value
);
52 base::MD5Digest digest
;
53 base::MD5Final(&digest
, &ctx
);
54 return base::StringToLowerASCII(base::HexEncode(digest
.a
, sizeof(digest
.a
)));
57 base::FilePath
GetConfigPath() {
58 std::string filename
= "host#" + GetMd5(net::GetHostName()) + ".json";
59 base::FilePath homedir
;
60 PathService::Get(base::DIR_HOME
, &homedir
);
61 return homedir
.Append(".config/chrome-remote-desktop").Append(filename
);
64 bool GetScriptPath(base::FilePath
* result
) {
65 base::FilePath
candidate_exe(kDaemonScript
);
66 if (access(candidate_exe
.value().c_str(), X_OK
) == 0) {
67 *result
= candidate_exe
;
73 bool RunHostScriptWithTimeout(
74 const std::vector
<std::string
>& args
,
75 base::TimeDelta timeout
,
79 // As long as we're relying on running an external binary from the
80 // PATH, don't do it as root.
82 LOG(ERROR
) << "Refusing to run script as root.";
85 base::FilePath script_path
;
86 if (!GetScriptPath(&script_path
)) {
87 LOG(ERROR
) << "GetScriptPath() failed.";
90 base::CommandLine
command_line(script_path
);
91 for (unsigned int i
= 0; i
< args
.size(); ++i
) {
92 command_line
.AppendArg(args
[i
]);
95 // Redirect the child's stdout to the parent's stderr. In the case where this
96 // parent process is a Native Messaging host, its stdout is used to send
97 // messages to the web-app.
98 base::FileHandleMappingVector fds_to_remap
;
99 fds_to_remap
.push_back(std::pair
<int, int>(STDERR_FILENO
, STDOUT_FILENO
));
100 base::LaunchOptions options
;
101 options
.fds_to_remap
= &fds_to_remap
;
103 #if !defined(OS_CHROMEOS)
104 options
.allow_new_privs
= true;
107 base::Process process
= base::LaunchProcess(command_line
, options
);
108 if (!process
.IsValid()) {
109 LOG(ERROR
) << "Failed to run command: "
110 << command_line
.GetCommandLineString();
114 if (!process
.WaitForExitWithTimeout(timeout
, exit_code
)) {
115 process
.Terminate(0, false);
116 LOG(ERROR
) << "Timeout exceeded for command: "
117 << command_line
.GetCommandLineString();
124 bool RunHostScript(const std::vector
<std::string
>& args
, int* exit_code
) {
125 return RunHostScriptWithTimeout(
126 args
, base::TimeDelta::FromMilliseconds(kDaemonTimeoutMs
), exit_code
);
131 DaemonControllerDelegateLinux::DaemonControllerDelegateLinux() {
134 DaemonControllerDelegateLinux::~DaemonControllerDelegateLinux() {
137 DaemonController::State
DaemonControllerDelegateLinux::GetState() {
138 base::FilePath script_path
;
139 if (!GetScriptPath(&script_path
)) {
140 LOG(ERROR
) << "GetScriptPath() failed.";
141 return DaemonController::STATE_UNKNOWN
;
143 base::CommandLine
command_line(script_path
);
144 command_line
.AppendArg("--get-status");
148 if (!base::GetAppOutputWithExitCode(command_line
, &status
, &exit_code
) ||
150 LOG(ERROR
) << "Failed to run \"" << command_line
.GetCommandLineString()
151 << "\". Exit code: " << exit_code
;
152 return DaemonController::STATE_UNKNOWN
;
155 base::TrimWhitespaceASCII(status
, base::TRIM_ALL
, &status
);
157 if (status
== "STARTED") {
158 return DaemonController::STATE_STARTED
;
159 } else if (status
== "STOPPED") {
160 return DaemonController::STATE_STOPPED
;
161 } else if (status
== "NOT_IMPLEMENTED") {
162 // Chrome Remote Desktop is not currently supported on the underlying Linux
164 return DaemonController::STATE_NOT_IMPLEMENTED
;
166 LOG(ERROR
) << "Unknown status string returned from \""
167 << command_line
.GetCommandLineString()
169 return DaemonController::STATE_UNKNOWN
;
173 scoped_ptr
<base::DictionaryValue
> DaemonControllerDelegateLinux::GetConfig() {
174 scoped_ptr
<base::DictionaryValue
> config(
175 HostConfigFromJsonFile(GetConfigPath()));
179 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue());
181 if (config
->GetString(kHostIdConfigPath
, &value
)) {
182 result
->SetString(kHostIdConfigPath
, value
);
184 if (config
->GetString(kXmppLoginConfigPath
, &value
)) {
185 result
->SetString(kXmppLoginConfigPath
, value
);
187 return result
.Pass();
190 void DaemonControllerDelegateLinux::SetConfigAndStart(
191 scoped_ptr
<base::DictionaryValue
> config
,
193 const DaemonController::CompletionCallback
& done
) {
194 // Add the user to chrome-remote-desktop group first.
195 std::vector
<std::string
> args
;
196 args
.push_back("--add-user");
198 if (!RunHostScriptWithTimeout(
199 args
, base::TimeDelta::FromSeconds(kSudoTimeoutSeconds
),
202 LOG(ERROR
) << "Failed to add user to chrome-remote-desktop group.";
203 done
.Run(DaemonController::RESULT_FAILED
);
207 // Ensure the configuration directory exists.
208 base::FilePath config_dir
= GetConfigPath().DirName();
209 if (!base::DirectoryExists(config_dir
) &&
210 !base::CreateDirectory(config_dir
)) {
211 LOG(ERROR
) << "Failed to create config directory " << config_dir
.value();
212 done
.Run(DaemonController::RESULT_FAILED
);
217 if (!HostConfigToJsonFile(*config
, GetConfigPath())) {
218 LOG(ERROR
) << "Failed to update config file.";
219 done
.Run(DaemonController::RESULT_FAILED
);
223 // Finally start the host.
225 args
.push_back("--start");
226 DaemonController::AsyncResult result
= DaemonController::RESULT_FAILED
;
227 if (RunHostScript(args
, &exit_code
) && (exit_code
== 0))
228 result
= DaemonController::RESULT_OK
;
233 void DaemonControllerDelegateLinux::UpdateConfig(
234 scoped_ptr
<base::DictionaryValue
> config
,
235 const DaemonController::CompletionCallback
& done
) {
236 scoped_ptr
<base::DictionaryValue
> new_config(
237 HostConfigFromJsonFile(GetConfigPath()));
239 new_config
->MergeDictionary(config
.get());
240 if (!new_config
|| !HostConfigToJsonFile(*new_config
, GetConfigPath())) {
241 LOG(ERROR
) << "Failed to update config file.";
242 done
.Run(DaemonController::RESULT_FAILED
);
246 std::vector
<std::string
> args
;
247 args
.push_back("--reload");
249 DaemonController::AsyncResult result
= DaemonController::RESULT_FAILED
;
250 if (RunHostScript(args
, &exit_code
) && (exit_code
== 0))
251 result
= DaemonController::RESULT_OK
;
256 void DaemonControllerDelegateLinux::Stop(
257 const DaemonController::CompletionCallback
& done
) {
258 std::vector
<std::string
> args
;
259 args
.push_back("--stop");
261 DaemonController::AsyncResult result
= DaemonController::RESULT_FAILED
;
262 if (RunHostScript(args
, &exit_code
) && (exit_code
== 0))
263 result
= DaemonController::RESULT_OK
;
268 DaemonController::UsageStatsConsent
269 DaemonControllerDelegateLinux::GetUsageStatsConsent() {
270 // Crash dump collection is not implemented on Linux yet.
271 // http://crbug.com/130678.
272 DaemonController::UsageStatsConsent consent
;
273 consent
.supported
= false;
274 consent
.allowed
= false;
275 consent
.set_by_policy
= false;
279 scoped_refptr
<DaemonController
> DaemonController::Create() {
280 scoped_ptr
<DaemonController::Delegate
> delegate(
281 new DaemonControllerDelegateLinux());
282 return new DaemonController(delegate
.Pass());
285 } // namespace remoting