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/kill.h"
22 #include "base/process/launch.h"
23 #include "base/process/process_handle.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_split.h"
26 #include "base/strings/string_util.h"
27 #include "base/thread_task_runner_handle.h"
28 #include "base/values.h"
29 #include "build/build_config.h"
30 #include "net/base/net_util.h"
31 #include "remoting/host/host_config.h"
32 #include "remoting/host/usage_stats_consent.h"
38 const char kDaemonScript
[] =
39 "/opt/google/chrome-remote-desktop/chrome-remote-desktop";
41 // Timeout for running daemon script. The script itself sets a timeout when
42 // waiting for the host to come online, so the setting here should be at least
44 const int64 kDaemonTimeoutMs
= 60000;
46 // Timeout for commands that require password prompt - 5 minutes.
47 const int64 kSudoTimeoutSeconds
= 5 * 60;
49 std::string
GetMd5(const std::string
& value
) {
52 base::MD5Update(&ctx
, value
);
53 base::MD5Digest digest
;
54 base::MD5Final(&digest
, &ctx
);
55 return base::StringToLowerASCII(base::HexEncode(digest
.a
, sizeof(digest
.a
)));
58 base::FilePath
GetConfigPath() {
59 std::string filename
= "host#" + GetMd5(net::GetHostName()) + ".json";
60 base::FilePath homedir
;
61 PathService::Get(base::DIR_HOME
, &homedir
);
62 return homedir
.Append(".config/chrome-remote-desktop").Append(filename
);
65 bool GetScriptPath(base::FilePath
* result
) {
66 base::FilePath
candidate_exe(kDaemonScript
);
67 if (access(candidate_exe
.value().c_str(), X_OK
) == 0) {
68 *result
= candidate_exe
;
74 bool RunHostScriptWithTimeout(
75 const std::vector
<std::string
>& args
,
76 base::TimeDelta timeout
,
80 // As long as we're relying on running an external binary from the
81 // PATH, don't do it as root.
83 LOG(ERROR
) << "Refusing to run script as root.";
86 base::FilePath script_path
;
87 if (!GetScriptPath(&script_path
)) {
88 LOG(ERROR
) << "GetScriptPath() failed.";
91 base::CommandLine
command_line(script_path
);
92 for (unsigned int i
= 0; i
< args
.size(); ++i
) {
93 command_line
.AppendArg(args
[i
]);
96 // Redirect the child's stdout to the parent's stderr. In the case where this
97 // parent process is a Native Messaging host, its stdout is used to send
98 // messages to the web-app.
99 base::FileHandleMappingVector fds_to_remap
;
100 fds_to_remap
.push_back(std::pair
<int, int>(STDERR_FILENO
, STDOUT_FILENO
));
101 base::LaunchOptions options
;
102 options
.fds_to_remap
= &fds_to_remap
;
104 #if !defined(OS_CHROMEOS)
105 options
.allow_new_privs
= true;
108 base::Process process
= base::LaunchProcess(command_line
, options
);
109 if (!process
.IsValid()) {
110 LOG(ERROR
) << "Failed to run command: "
111 << command_line
.GetCommandLineString();
115 if (!process
.WaitForExitWithTimeout(timeout
, exit_code
)) {
116 base::KillProcess(process
.Handle(), 0, false);
117 LOG(ERROR
) << "Timeout exceeded for command: "
118 << command_line
.GetCommandLineString();
125 bool RunHostScript(const std::vector
<std::string
>& args
, int* exit_code
) {
126 return RunHostScriptWithTimeout(
127 args
, base::TimeDelta::FromMilliseconds(kDaemonTimeoutMs
), exit_code
);
132 DaemonControllerDelegateLinux::DaemonControllerDelegateLinux() {
135 DaemonControllerDelegateLinux::~DaemonControllerDelegateLinux() {
138 DaemonController::State
DaemonControllerDelegateLinux::GetState() {
139 base::FilePath script_path
;
140 if (!GetScriptPath(&script_path
)) {
141 LOG(ERROR
) << "GetScriptPath() failed.";
142 return DaemonController::STATE_UNKNOWN
;
144 base::CommandLine
command_line(script_path
);
145 command_line
.AppendArg("--get-status");
149 if (!base::GetAppOutputWithExitCode(command_line
, &status
, &exit_code
) ||
151 LOG(ERROR
) << "Failed to run \"" << command_line
.GetCommandLineString()
152 << "\". Exit code: " << exit_code
;
153 return DaemonController::STATE_UNKNOWN
;
156 base::TrimWhitespaceASCII(status
, base::TRIM_ALL
, &status
);
158 if (status
== "STARTED") {
159 return DaemonController::STATE_STARTED
;
160 } else if (status
== "STOPPED") {
161 return DaemonController::STATE_STOPPED
;
162 } else if (status
== "NOT_IMPLEMENTED") {
163 // Chrome Remote Desktop is not currently supported on the underlying Linux
165 return DaemonController::STATE_NOT_IMPLEMENTED
;
167 LOG(ERROR
) << "Unknown status string returned from \""
168 << command_line
.GetCommandLineString()
170 return DaemonController::STATE_UNKNOWN
;
174 scoped_ptr
<base::DictionaryValue
> DaemonControllerDelegateLinux::GetConfig() {
175 scoped_ptr
<base::DictionaryValue
> config(
176 HostConfigFromJsonFile(GetConfigPath()));
180 scoped_ptr
<base::DictionaryValue
> result(new base::DictionaryValue());
182 if (config
->GetString(kHostIdConfigPath
, &value
)) {
183 result
->SetString(kHostIdConfigPath
, value
);
185 if (config
->GetString(kXmppLoginConfigPath
, &value
)) {
186 result
->SetString(kXmppLoginConfigPath
, value
);
188 return result
.Pass();
191 void DaemonControllerDelegateLinux::SetConfigAndStart(
192 scoped_ptr
<base::DictionaryValue
> config
,
194 const DaemonController::CompletionCallback
& done
) {
195 // Add the user to chrome-remote-desktop group first.
196 std::vector
<std::string
> args
;
197 args
.push_back("--add-user");
199 if (!RunHostScriptWithTimeout(
200 args
, base::TimeDelta::FromSeconds(kSudoTimeoutSeconds
),
203 LOG(ERROR
) << "Failed to add user to chrome-remote-desktop group.";
204 done
.Run(DaemonController::RESULT_FAILED
);
208 // Ensure the configuration directory exists.
209 base::FilePath config_dir
= GetConfigPath().DirName();
210 if (!base::DirectoryExists(config_dir
) &&
211 !base::CreateDirectory(config_dir
)) {
212 LOG(ERROR
) << "Failed to create config directory " << config_dir
.value();
213 done
.Run(DaemonController::RESULT_FAILED
);
218 if (!HostConfigToJsonFile(*config
, GetConfigPath())) {
219 LOG(ERROR
) << "Failed to update config file.";
220 done
.Run(DaemonController::RESULT_FAILED
);
224 // Finally start the host.
226 args
.push_back("--start");
227 DaemonController::AsyncResult result
= DaemonController::RESULT_FAILED
;
228 if (RunHostScript(args
, &exit_code
) && (exit_code
== 0))
229 result
= DaemonController::RESULT_OK
;
234 void DaemonControllerDelegateLinux::UpdateConfig(
235 scoped_ptr
<base::DictionaryValue
> config
,
236 const DaemonController::CompletionCallback
& done
) {
237 scoped_ptr
<base::DictionaryValue
> new_config(
238 HostConfigFromJsonFile(GetConfigPath()));
240 new_config
->MergeDictionary(config
.get());
241 if (!new_config
|| !HostConfigToJsonFile(*new_config
, GetConfigPath())) {
242 LOG(ERROR
) << "Failed to update config file.";
243 done
.Run(DaemonController::RESULT_FAILED
);
247 std::vector
<std::string
> args
;
248 args
.push_back("--reload");
250 DaemonController::AsyncResult result
= DaemonController::RESULT_FAILED
;
251 if (RunHostScript(args
, &exit_code
) && (exit_code
== 0))
252 result
= DaemonController::RESULT_OK
;
257 void DaemonControllerDelegateLinux::Stop(
258 const DaemonController::CompletionCallback
& done
) {
259 std::vector
<std::string
> args
;
260 args
.push_back("--stop");
262 DaemonController::AsyncResult result
= DaemonController::RESULT_FAILED
;
263 if (RunHostScript(args
, &exit_code
) && (exit_code
== 0))
264 result
= DaemonController::RESULT_OK
;
269 DaemonController::UsageStatsConsent
270 DaemonControllerDelegateLinux::GetUsageStatsConsent() {
271 // Crash dump collection is not implemented on Linux yet.
272 // http://crbug.com/130678.
273 DaemonController::UsageStatsConsent consent
;
274 consent
.supported
= false;
275 consent
.allowed
= false;
276 consent
.set_by_policy
= false;
280 scoped_refptr
<DaemonController
> DaemonController::Create() {
281 scoped_ptr
<DaemonController::Delegate
> delegate(
282 new DaemonControllerDelegateLinux());
283 return new DaemonController(delegate
.Pass());
286 } // namespace remoting